﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Douban.Api.User;
using Douban.Core;
using Douban.Core.Events;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.ServiceLocation;

namespace Douban.Modules.Friends.ViewModels
{
    public class ContactListItemViewModel : ObservableObject
    {
        private readonly UserDetail _user;
        private ICommand _sendMessageCommand;
        private ICommand _checkInfoCommand;

        public ContactListItemViewModel(UserDetail user)
        {
            _user = user;
        }

        public string Name
        {
            get
            {
                return _user.Name;
            }
            set
            {
                if(_user.Name != value)
                {
                    _user.Name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
        
        public string DoubanPage
        {
            get { return _user.DoubanPage; }
            set
            {
                if (_user.DoubanPage != value)
                {
                    _user.DoubanPage = value;
                    NotifyPropertyChanged("DoubanPage");
                }
            }
        }
        
        public string Signature
        {
            get { return _user.Signature; }
            set
            {
                if (_user.Signature != value)
                {
                    _user.Signature = value;
                    NotifyPropertyChanged("Signature");
                }
            }
        }
        
        public string IconUri
        {
            get { return _user.IconUri; }
            set
            {
                if (_user.IconUri != value)
                {
                    _user.IconUri = value;
                    NotifyPropertyChanged("IconUri");
                }
            }
        }
	
        public ICommand SendMessageCommand
        {
            get
            {
                if (_sendMessageCommand == null)
                    _sendMessageCommand = new DelegateCommand<object>(OnSendMessage);

                return _sendMessageCommand;
            }
        }

        public ICommand CheckInfoCommand 
        {
            get
            {
                if (_checkInfoCommand == null)
                    _checkInfoCommand = new DelegateCommand<object>(OnCheckInfo);

                return _checkInfoCommand;
            }
        }

        private void OnCheckInfo(object parameter)
        {
            IEventAggregator eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<CheckInfoEvent>().Publish(_user.Id);
        }

        private void OnSendMessage(object parameter)
        {
            IEventAggregator eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<ContactDoubleClickedEvent>().Publish(_user.Id);
        }
    }
}
